<?php

namespace App\Admin\Controllers;

use App\Models\Grade;
use App\Models\Member;
use App\Models\Program;
use App\Models\Task;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;

class TaskController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }
    public function getState(){
        $mess=$_SERVER['REQUEST_URI'];
        if(strpos($mess,'no')) {
            return $state = 0;
        }elseif(strpos($mess,'yes')){
            return $state=1;
        }
    }
    public function getBranch(){
        $id=Admin::user()->id;
        $member=Member::where('id',$id)->first();
        return $member['branch_id'];
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $state=$this->getState();
        $branch=$this->getBranch();
        if($branch==3||$branch==4||$branch==5||$branch==6){
            $task = new Grid(new Task);
            $task->disableActions();
            $task->model()->where('state',$state);
            $task->model()->where('branch_id',$branch);
            $task->id('Id');
            $task->column('program.name','所属项目');
            $task->column('member.name','所属成员');
            $task->time('评估时间')->editable();
            $states = [
                'on'  => ['value' => 0, 'text' => '未处理', 'color' => 'danger'],
                'off' => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            ];
            $task->state('处理状态')->switch($states);
            $task->updated_at('Updated at');
            return $task;
        }else{
            $program = new Grid(new Program);
            $program->disableActions();
            $program->model()->where('pass',$state);
            $program->id('Id');
            $program->name('项目名');
            $program->time('跨越时间');
            $program->column( 'wd','武当评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===5){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $program->column( 'gm','古墓评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===4){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $program->column( 'xy','逍遥评估时间')->display(function (){
                $time = Grade::all()->toArray();
                $test=0;
                foreach ($time as $value){
                    if($value["program_id"]===$this->id && $value['branch_id']===3){
                        $test+=$value['usetime'];
                    }
                    if($value["program_id"]===$this->id && $value['branch_id']===6){
                        $test+=$value['usetime'];
                    }
                }
                return $test;
            });
            $program->plan('当前进度');
            $states = [
                'on'  => ['value' => 0, 'text' => '未处理', 'color' => 'danger'],
                'off' => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            ];
            $program->pass('处理状态')->switch($states);
            $program->ratiowd('武当绩效比例')->editable();
            $program->ratiogm('古墓绩效比例')->editable();
            $program->ratioxy('逍遥绩效比例')->editable();
            $program->money('绩效奖金')->editable();
            $program->created_at('创建时间');
            $program->updated_at('更新时间');
            return $program;
        }

    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Task::findOrFail($id));

        $show->id('Id');
        $show->program_id('Program id');
        $show->master_id('Master id');
        $show->member_id('Member id');
        $show->time('Time');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $branch=$this->getBranch();
        if($branch==3||$branch==4||$branch==5||$branch==6) {
            $task = new Form(new Task);
            $task->number('program_id', 'Program id');
            $task->number('master_id', 'Master id');
            $task->number('member_id', 'Member id');
            $task->number('time', 'Time');
            $states = [
                'on'  => ['value' => 0, 'text' => '未处理', 'color' => 'danger'],
                'off' => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            ];
            $task->switch('state', '项目状态')->states($states);
            return $task;

        }else{
            $program = new Form(new Program);
            $program->text('name', '项目名');
            $program->multipleSelect('members','参与成员')->options(Member::where('state',1)->pluck('name', 'id'));
            $program->text('usertime', '用时');
            $program->text('time', '时间跨度');
            $program->text('money', '绩效奖金');
            $program->text('ratiowd', '武当绩效占比');
            $program->text('ratiogm', '古墓绩效占比');
            $program->text('ratioxy', '逍遥绩效占比');
            $states = [
                'on'  => ['value' => 0, 'text' => '未处理', 'color' => 'danger'],
                'off' => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            ];

            $program->switch('pass','项目状态')->states($states);
            $program->text('plan','当前进度')->disable();
            return $program;
        }
    }
}
